﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityTouchTable.Runtime;

namespace UnityTouchTable.Example
{
    /// <summary>
    /// 箭头节点
    /// </summary>
    [RequireComponent(typeof(SpriteRenderer))]
    [DisallowMultipleComponent]
    public class ArrowNode : MonoBehaviour, IAnglehandle
    {
        #region const
        /// <summary>
        /// 主颜色
        /// </summary>
        private const string MAINCOLOR = "_MainColor";
        #endregion

        #region Field
        /// <summary>
        /// 当移动时是否更新角度
        /// </summary>
        [SerializeField]
        [Header("当移动时是否更新角度")]
        private bool m_OnMoveUpdateAngle = false;

        /// <summary>
        /// 材质球
        /// </summary>
        [SerializeField]
        [Header("材质球")]
        private Material m_Material = default;

        /// <summary>
        /// 半径
        /// </summary>
        [SerializeField]
        [Header("半径")]
        private float m_Radius = 1.7f;

        /// <summary>
        /// 渐变颜色
        /// </summary>
        [SerializeField]
        [Header("渐变颜色")]
        private Gradient m_Gradient = default;
        #endregion

        #region Property
        /// <summary>
        /// 获取当移动时是否更新角度
        /// </summary>
        public bool OnMoveUpdateAngle => this.m_OnMoveUpdateAngle;
        #endregion

        #region Function
        /// <summary>
        /// 更新角度
        /// </summary>
        /// <param name="angle">角度</param>
        public void UpdateAngle(float angle)
        {
            //位置计算
            float radian = Mathf.Deg2Rad * angle;
            float x = Mathf.Cos(radian) * this.m_Radius;
            float y = Mathf.Sin(radian) * this.m_Radius;
            Vector3 localPostion = new Vector3(x, y, 0.0f);
            this.transform.localPosition = localPostion;

            angle += 90.0f;
            if (angle < 0)
                angle += 360.0f;
            //旋转计算
            this.transform.localEulerAngles = Vector3.back * angle;

            //颜色更新
            this.m_Material.SetColor(ArrowNode.MAINCOLOR, this.m_Gradient.Evaluate(angle / 360.0f));
        }
        #endregion
    }
}